<template>
  <div class="create-or-edit">
    <el-card>
      <div slot="header">
        <el-steps
          :active="activeStep"
          simple>
          <el-step
            v-for="(item, i) in step"
            :key="item.id"
            :title="item.title"
            :icon="item.icon"
            @click.native="activeStep = i"
          ></el-step>
        </el-steps>
      </div>
      <el-form>
        <div v-show="activeStep === 0">
          <el-form-item label="名称">
            <el-input v-model="course.courseName"></el-input>
          </el-form-item>
          <el-form-item label="简介">
            <el-input v-model="course.brief"></el-input>
          </el-form-item>
          <el-form-item label="讲师名字">
            <el-input v-model="course.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="职位">
            <el-input v-model="course.teacherDTO.position"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input v-model="course.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程概述">
            <el-input
              v-model="course.previewFirstField"
              style="width: 45%;margin-right:30px"
            ></el-input>
            <el-input
              v-model="course.previewSecondField"
              style="width: 45%;"
            ></el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 1">
          <image-upload
            v-model="course.courseListImg"
            label="课程封面"
            :limit="3"
          ></image-upload>
          <image-upload
            v-model="course.courseImgUrl"
            label="解锁封面"
            :limit="5"
          ></image-upload>
          <el-form-item label="课程排序">
             <el-input-number
              v-model="course.sortNum"
              controls-position="right"></el-input-number>
          </el-form-item>
        </div>
        <div v-show="activeStep === 2">
          <el-form-item label="销售价格">
            <el-input
              v-model="course.discounts"
              type="number"
              :min="0"
              placeholder="请输入内容">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input
              v-model="course.price"
              type="number"
              :min="0"
              placeholder="请输入内容">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input
              v-model="course.sales"
              type="number"
              :min="0"
              placeholder="请输入内容">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input
              v-model="course.discountsTag"
              type="text"
              placeholder="请输入内容"
              maxlength="4"
              show-word-limit
            >
            </el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 3">
          <el-form-item label="秒杀活动">
            <el-switch
              v-model="course.activityCourse"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <template v-if="course.activityCourse">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="course.activityCourseDTO.beginTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="course.activityCourseDTO.endTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="秒杀价">
              <el-input
                v-model="course.activityCourseDTO.amount"
                type="number"
                :min="0"
                placeholder="请输入内容">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="秒杀库存">
              <el-input
                v-model="course.activityCourseDTO.stock"
                type="number"
                :min="0"
                placeholder="请输入内容">
                <template slot="append">个</template>
              </el-input>
            </el-form-item>
          </template>
        </div>
        <div v-show="activeStep === 4">
          <el-form-item label="课程详情">
            <text-editor v-model="course.courseDescriptionMarkDown"></text-editor>
          </el-form-item>
          <el-form-item label="是否上架">
            <el-switch
              v-model="course.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="handleSaveCourse"
              type="primary">确定</el-button>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button
            @click="activeStep++"
            v-show="activeStep !== step.length - 1"
          >下一步</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
// 引入上传图片组件
import ImageUpload from './ImageUpload'
// 引入富文本组件
import textEditor from '@/components/TextEditor/index'
export default {
  name: 'createOrEdit',
  props: {
    courseId: {
      type: [Number, String]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  created () {
    if (this.isEdit) {
      this.loadCourse()
    }
  },
  data () {
    return {
      activeStep: 0,
      step: [
        { id: 1, title: '基本信息', icon: 'el-icon-edit' },
        { id: 2, title: '课程封面', icon: 'el-icon-edit' },
        { id: 3, title: '销售信息', icon: 'el-icon-edit' },
        { id: 4, title: '秒杀活动', icon: 'el-icon-edit' },
        { id: 5, title: '课程详情', icon: 'el-icon-edit' }
      ],
      formLabelAlign: [],
      course: {
        // id: 0,
        courseName: '',
        brief: '',
        teacherDTO: {
          // id: 0,
          // courseId: 0,
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '',
        // 商品原价
        price: 0,
        // 优惠价
        discounts: 0,
        priceTag: '',
        // 活动标签
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        courseListImg: '',
        courseImgUrl: '',
        sortNum: 0,
        previewFirstField: '',
        previewSecondField: '',
        // 是否上架
        status: 0,
        // 销量
        sales: 0,
        // 是否做秒杀活动
        activityCourse: true,
        activityCourseDTO: {
          // id: 0,
          // courseId: 0,
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0
        },
        autoOnlineTime: ''
      }
    }
  },
  components: {
    ImageUpload,
    textEditor
  },
  methods: {
    async handleSaveCourse () {
      const { data } = await saveOrUpdateCourse(this.course)
      if (data.code === '000000') {
        this.$message.success(`${this.isEdit ? '编辑' : '新增'}课程成功！`)
        this.$router.push({
          name: 'course'
        })
      }
    },
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        this.course = data.data
        // 编辑时非秒杀课程，需要给初始化 activityCourseDTO 结构
        if (!data.data.activityCourseDTO) {
          this.course.activityCourseDTO = {
            beginTime: '',
            endTime: '',
            amount: 0,
            stock: 0
          }
        }
      }
    }
  }
}

</script>
<style lang='scss' scoped>
.el-step {
  cursor: pointer;
}
</style>
